function MainMenu()
{
	this.menus = new Array()
	this.selectedIndex = 0;
}

MainMenu.prototype.push = function(menu)
{
	this.menus.push(menu)
}

MainMenu.prototype.display = function()
{
	var ch = new Array();
	for(var i=0; i<this.menus.length; i++)
	{
		ch.push(this.menus[i].display(i))
	}
	document.getElementById("home_menu_list").innerHTML = ch.join("");
	document.getElementById("home_menu_content").setAttribute("items", this.menus.length)
	if(this.menus.length>0)
	{
		this.select(0)
	}
	
	var thisObject = this;
	document.getElementById("home_menu_list").onclick = function(event)
	{
		if(window.event)
		{
			target = window.event.srcElement
		}
		else
		{
			target = event.target
		}
		while(target.tagName != "LI" && target != null && target.getAttribute("menu_index")==null)
		{
			target = target.parentNode;
		}
		if(target==null || target.getAttribute("menu_index")==null)
		{
			return
		}
		thisObject.select(parseInt(target.getAttribute("menu_index")))
	}
}

MainMenu.prototype.select = function(index)
{
	if(isNaN(index) || index<0 || index>=this.menus.length)
	{
		return false;
	}
	if(document.getElementById("home_menu_"+this.menus[this.selectedIndex].id))
	{
		document.getElementById("home_menu_"+this.menus[this.selectedIndex].id).removeAttribute("selected")
	}
	this.selectedIndex = index;
	document.getElementById("home_menu_"+this.menus[this.selectedIndex].id).setAttribute("selected", 1)
	document.getElementById("home_menu_title").innerHTML = this.menus[this.selectedIndex].label
	this.menus[this.selectedIndex].displayPanel()
}
function Menu(id, label)
{
	this.id = id;
	this.label = label;
	
	this.selected = null;
	this.panel = null;
}

Menu.prototype.display = function(index)
{
	return "<li id='home_menu_"+this.id+"' menu_index="+index+"><div>"+this.label+"</div></li>"
}

Menu.prototype.displayPanel = function()
{
	document.getElementById("home_menu_content").innerHTML = "";
	var thisObject = this;
	if(this.panel == null)
	{
		document.getElementById("home_menu_content").setAttribute("loading", 1);
		ajax.sendAsync("./action/commun/menu.php5?action=display&panel="+this.id+"&u="+new Date().getTime(), function(datas)
		{
			thisObject.panel = ajax.eval(datas)
			document.getElementById("home_menu_content").removeAttribute("loading")
			document.getElementById("home_menu_content").innerHTML = thisObject.panel
			
			thisObject.select(document.getElementById(thisObject.panel.match(/\id\=\"([^"]+)\" menu=/)[1]))
		})
	}
	else
	{
		document.getElementById("home_menu_content").innerHTML = this.panel
		this.select(document.getElementById(this.selected))
	}
	document.getElementById("home_menu_content").onclick = function(event)
	{
		if(window.event)
		{
			target = window.event.srcElement
		}
		else
		{
			target = event.target
		}
		var limit = document.getElementById("home_menu_content");
		while(target!=null && target!=limit)
		{
			if(target.getAttribute("menu")!=null)
			{
				break;
			}
			target = target.parentNode;
		}
		if(target.getAttribute("menu")==null) return;
		thisObject.select(target)
	}
}

Menu.prototype.select = function(elem)
{
	if(elem==null) return
	if(elem.getAttribute("menu")==null) return
	if(this.selected!=null)
	{
		if(document.getElementById(this.selected)!=null)
		{
			document.getElementById(this.selected).removeAttribute("selected")
		}
	}
	this.selected = elem.id
	elem.setAttribute("selected", true);
	document.getElementById("home_content_title").innerHTML = this.label + " » " + elem.innerHTML
	document.getElementById("home_content_body").setAttribute("loading", true)
	document.getElementById("home_content_body").innerHTML = ""
	ajax.sendAsync("./action/commun/menu.php5?action=select&menu="+this.id+"&submenu="+elem.getAttribute("menu")+"&u="+new Date().getTime(), function(datas)
	{
		document.getElementById("home_content_body").removeAttribute("loading", true)
		var str = ajax.eval(datas)
		setHtml(document.getElementById("home_content_body"), str);
	})
}
